package com.cx.lambda;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author zhangshuai
 * @version V1.0
 * @Project Java2_26
 * @Package com.cx.anno
 * @Description:
 * @Date 2025/5/22 14:02
 */

public class LambdaTest {
    public static void main(String[] args) {
        //lambda表达式   简化匿名内部类的写法,主要应用于函数式接口（只有一个抽象方法的函数）
        //语法：(参数列表) -> { 方法体 }

        Collections.sort(new ArrayList<>(),new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        //外部比较器
        Comparator comparator = new Comparator<Integer>(){
           @Override
           public int compare(Integer o1, Integer o2) {
               return o1 - o2;
           }
       };
        //外部比较器的lambda表达式写法
        Comparator<Integer> comparator1 =(o1,o2)->o1-o2;

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(21);
        list.add(31);
        list.add(4);
        Collections.sort(list,(o1,o2)->o2-o1);
        System.out.println(list);

        //线程
        Runnable runnable = new Runnable() {
             @Override
             public void run() {
                 System.out.println("线程1");
             }
         };
        Runnable run =()-> System.out.println("线程2");System.out.println("线程3");


        //函数式接口
      MyInterface myInterface  =  new MyInterface() {
            @Override
            public void test(int id, String name) {
                System.out.println("id:"+id+"name:"+name);
            }
        };
        //lambda表达式写法,主要是为了简化匿名内部类的写法
        MyInterface myInterface1 = (id,name)-> System.out.println("id:"+id+"name:"+name);

    }
}
//函数式接口   只有一个抽象方法的函数
interface MyInterface{
    void test(int id,String name);
}
